%include "system.inc"
org 0x8000
bits 16
start:
	db 'NT'
	mov ax,piano
	mov bx,inst1
	mov cx,inst2
	mov dx,inst3
	call WIN_MSG_BOX
	call os_wait_for_key
	call win_set_up
	mov si,startmsg
	call win_print_string
    mov bl, 0x9F                        ; White block to draw keyboard on
    mov dh, 4
    mov dl, 5
    mov si, 64
    mov di, 21
    call os_draw_block
	mov dl, 24                ; Top line of box
    mov dh, 6
    call os_move_cursor

        mov ah, 0Eh
        mov al, 196

        mov cx, 31
.loop1:
        int 10h
        loop .loop1


        mov dl, 24                ; Bottom line of box
        mov dh, 18
        call os_move_cursor

        mov ah, 0Eh
        mov al, 196

        mov cx, 31
.loop2:
        int 10h
        loop .loop2



        mov dl, 23                ; Top-left corner
        mov dh, 6
        call os_move_cursor

        mov al, 218
        int 10h


        mov dl, 55                ; Top-right corner
        mov dh, 6
        call os_move_cursor

        mov al, 191
        int 10h


        mov dl, 23                ; Bottom-left corner
        mov dh, 18
        call os_move_cursor

        mov al, 192
        int 10h


        mov dl, 55                ; Bottom-right corner
        mov dh, 18
        call os_move_cursor

        mov al, 217
        int 10h


        mov dl, 23                ; Left-hand box line
        mov dh, 7
        mov al, 179
.loop3:
        call os_move_cursor
        int 10h
        inc dh
        cmp dh, 18
        jne .loop3


        mov dl, 55                ; Right-hand box line
        mov dh, 7
        mov al, 179
.loop4:
        call os_move_cursor
        int 10h
        inc dh
        cmp dh, 18
        jne .loop4


        mov dl, 23                ; Key-separating lines
.biggerloop:
        add dl, 4
        mov dh, 7
        mov al, 179
.loop5:
        call os_move_cursor
        int 10h
        inc dh
        cmp dh, 18
        jne .loop5
        cmp dl, 51
        jne .biggerloop


        mov al, 194                ; Top of box line joiners
        mov dh, 6
        mov dl, 27
.loop6:
        call os_move_cursor
        int 10h
        add dl, 4
        cmp dl, 55
        jne .loop6


        mov al, 193                ; Bottom of box line joiners
        mov dh, 18
        mov dl, 27
.loop7:
        call os_move_cursor
        int 10h
        add dl, 4
        cmp dl, 55
        jne .loop7


        ; And now for the black keys...

        mov bl, WHITE_ON_BLACK

        mov dh, 6
        mov dl, 26
        mov si, 3
        mov di, 13
        call os_draw_block
        
        mov dh, 6
        mov dl, 30
        mov si, 3
        mov di, 13
        call os_draw_block
        
        mov dh, 6
        mov dl, 38
        mov si, 3
        mov di, 13
        call os_draw_block
        
        mov dh, 6
        mov dl, 42
        mov si, 3
        mov di, 13
        call os_draw_block
        
        mov dh, 6
        mov dl, 46
        mov si, 3
        mov di, 13
        call os_draw_block



        ; And lastly, draw the labels on the keys indicating which
        ; (computer!) keys to press to get notes
        
        mov ah, 0Eh

        mov dh, 17
        mov dl, 25
        call os_move_cursor

        mov al, 'Z'
        int 10h

        add dl, 4
        call os_move_cursor
        mov al, 'X'
        int 10h

        add dl, 4
        call os_move_cursor
        mov al, 'C'
        int 10h

        add dl, 4
        call os_move_cursor
        mov al, 'V'
        int 10h

        add dl, 4
        call os_move_cursor
        mov al, 'B'
        int 10h

        add dl, 4
        call os_move_cursor
        mov al, 'N'
        int 10h

        add dl, 4
        call os_move_cursor
        mov al, 'M'
        int 10h

        add dl, 4
        call os_move_cursor
        mov al, ','
        int 10h
 ; Phew! We've drawn all the keys now
		call mouse_init
		
.retry:
	call mouselib_freemove
	jc .nokey
	
	call mouselib_locate
	call mouselib_anyclick
	jc .check
	jmp .retry
.nokey:                                ; Matching keys with notes
        cmp al, 'z'
        jne .x
        mov ax, 4000
        mov bx, 0
        call os_speaker_tone
        jmp .retry

.x:
        cmp al, 'x'
        jne .c
        mov ax, 3600
        mov bx, 0
        call os_speaker_tone
        jmp .retry

.c:
        cmp al, 'c'
        jne .v
        mov ax, 3200
        mov bx, 0
        call os_speaker_tone
        jmp .retry


.v:
        cmp al, 'v'
        jne .b
        mov ax, 3000
        mov bx, 0
        call os_speaker_tone
        jmp .retry

.b:
        cmp al, 'b'
        jne .n
        mov ax, 2700
        mov bx, 0
        call os_speaker_tone
        jmp .retry

.n:
        cmp al, 'n'
        jne .m
        mov ax, 2400
        mov bx, 0
        call os_speaker_tone
        jmp .retry

.m:
        cmp al, 'm'
        jne .comma
        mov ax, 2100
        mov bx, 0
        call os_speaker_tone
        jmp .retry

.comma:
        cmp al, ','
        jne .space
        mov ax, 2000
        mov bx, 0
        call os_speaker_tone
        jmp .retry

.space:
        cmp al, ' '
        jne .q
        call os_speaker_off
        jmp .retry

.q:
        cmp al, 'q'
        je .end
        cmp al, 'Q'
        je .end
        jmp .nowt

.nowt:
        jmp .retry
.check:
	call mouse_check_close
	cmp dx,0x1F34
	je .end
	jmp .retry
.end:
        call os_speaker_off

        call os_clear_screen

        call os_show_cursor

        int 0x20                       ; Back to OS

	startmsg db 'POSx86 Music Keyboard. Press Q to Quit.',0
	close db '[X]',0
	inst1 db 'Welcome to POSx86 Piano!',0
	inst2 db 'Press Q or Click X to quit',0
	inst3 db 'Space to silence a note',0
	piano db 'PIANO.COM',0
%include "winlib/io/stdiowin.asm"
%include "winlib/mouse/mouse.asm"
%Include "winlib/graphic/graphics.asm"